package com.lhq.business.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lhq.business.domain.Book;
import com.lhq.business.service.IBookService;
import com.lhq.common.annotation.Anonymous;
import com.lhq.common.annotation.Log;
import com.lhq.common.core.controller.BaseController;
import com.lhq.common.core.domain.AjaxResult;
import com.lhq.common.enums.BusinessType;
import com.lhq.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 图书Controller
 *
 * @author lhq
 * @date 2024-05-11
 */
@RestController
@RequestMapping("/business/book")
public class BookController extends BaseController
{
    @Autowired
    private IBookService bookService;

    /**
     * 查询图书列表
     */
    @PreAuthorize("@ss.hasPermi('business:book:list')")
    @GetMapping("/list")
    public AjaxResult list(Book book,
                             @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize)
    {
        Page<Book> page =new Page<>(pageNum,pageSize);
        return AjaxResult.successPage(bookService.selectList(page,book));
    }

    /**
     * 导出图书列表
     */
    @PreAuthorize("@ss.hasPermi('business:book:export')")
    @Log(title = "图书", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Book book,
                       @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize)
    {
        Page<Book> page =new Page<>(pageNum,pageSize);
        List<Book> list = bookService.selectList(page,book).getRecords();
        ExcelUtil<Book> util = new ExcelUtil<Book>(Book.class);
        util.exportExcel(response, list, "图书数据");
    }

    /**
     * 获取图书详细信息
     */
    @Anonymous
    // @PreAuthorize("@ss.hasPermi('business:book:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(bookService.selectById(id));
    }

    /**
     * 新增图书
     */
    @PreAuthorize("@ss.hasPermi('business:book:add')")
    @Log(title = "图书", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Book book)
    {
        return toAjax(bookService.insertBook(book));
    }

    /**
     * 修改图书
     */
    @PreAuthorize("@ss.hasPermi('business:book:edit')")
    @Log(title = "图书", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody Book book)
    {
        return toAjax(bookService.updateBook(book));
    }

    /**
     * 删除图书
     */
    @PreAuthorize("@ss.hasPermi('business:book:remove')")
    @Log(title = "图书", businessType = BusinessType.DELETE)
	@GetMapping("/del/{id}")
    public AjaxResult remove(@PathVariable("id") String  id)
    {
        return toAjax(bookService.deleteById(id));
    }

    
}
